<?php
// google guide
// https://developers.google.com/youtube/2.0/developers_guide_protocol?hl=ja
// google oauth playground api
// https://developers.google.com/oauthplayground/?code=4/9FEOf1uFPWw0Qy7JmsuSXgQDMGfv.ot0e8kw5_V0dgrKXntQAax1mbK6ifAI
//Redirect URIs: 	http://vibes.agniuek.com/Oauth2callback
//JavaScript origins: 	http://vibes.agniuek.com
//https://code.google.com/apis/ajax/playground/?exp=youtube#change_the_playing_video
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata_YouTube');
require_once (APPLICATION_PATH . "/../library/UserAgentManager.class.php");
require_once (APPLICATION_PATH . "/../library/FeedUtil.class.php");
require_once (APPLICATION_PATH . "/../library/SecureManager.class.php");
session_start();
header("Access-Control-Allow-Origin: *");
$frontController = Zend_Controller_Front::getInstance();
$appConfig = $frontController->getParam('bootstrap')->getOptions();
//var_export($appConfig);
$userAgentManager = new UserAgentManager();
$is_smartphone = $userAgentManager->is_smartphone();
$feedUtil = new FeedUtil();
$secureManager = new SecureManager();

$feed_url = "";
$feed_url_param = "";
$feed_url_session = "";
$feed_share_url="";
//$default_feed="https://www.youtube.com/watch?v=7_3hKVxOcRI&list=PL88uZ99yWfPvScsoTtURL6S6LmoM0uBoR";
$playlistItems=array();
$grid_tag="";
$nextPageToken="";
$prevPageToken="";
$totalResults="";
$OAUTH2_CLIENT_ID = $appConfig['clientId'];
$OAUTH2_CLIENT_SECRET = $appConfig['clientSecret'];
$developerKey = $appConfig['developerKey'];
$applicationName = $appConfig['applicationName'];
$default_keyword="jazz";
$keyword="";
if (isset($_REQUEST['keyword'])) {
    $keyword=$_REQUEST['keyword'];
}
//if (isset($_SESSION['keyword'])) {
//    $keyword=$_SESSION['keyword'];
//}

// fix feed_url
if (isset($_REQUEST['feed_url'])) {
    $feed_url_param = $secureManager->sanitize($_REQUEST['feed_url']);
}
if (isset($_SESSION['feed_url'])) {
    $feed_url_session = $secureManager->sanitize( $_SESSION['feed_url']);
}

if (!empty($feed_url_param)) {
   $feed_url = $feed_url_param;
   $feed_share_url = 'http://' . $_SERVER['SERVER_NAME'] . '?feed_url=' . urlencode($feed_url);

}elseif (!empty($feed_url_session)) {
   $feed_url = $feed_url_session;
   $feed_share_url = 'http://' . $_SERVER['SERVER_NAME'] . '?feed_url=' . urlencode($feed_url);

}
if (empty($feed_url) && empty ($keyword)) {
    $keyword=$default_keyword;
}

$feed_share_url = 'http://' . $_SERVER['SERVER_NAME'] . '?feed_url=' . urlencode($feed_url);
//$playlistVideoFeed='ylLzyHk54Z0';
//$default_video_id='MILSirUni5E';

try {

    // feed share or reload
    if(!empty($keyword)){
        // keyword
        //echo "001";
        $playlistItems = $feedUtil->feedsFromKeyword($keyword, $OAUTH2_CLIENT_ID, $OAUTH2_CLIENT_SECRET ,$developerKey);
    }else if(!empty($feed_url)){
        // echo "002";
        $playlistItems = $feedUtil->feedsFromPlaylistOthers($feed_url, $OAUTH2_CLIENT_ID, $OAUTH2_CLIENT_SECRET ,$developerKey);
    }
//var_export($playlistItems);
    $grid_tag = $feedUtil->feedToHtml($playlistItems);
    $default_video_id=$feedUtil->getRandomVideoId($playlistItems);

    if(isset($playlistItems['pageInfo'])){
      $totalResults = $playlistItems['pageInfo']['totalResults'];
    }
    if( isset($playlistItems['nextPageToken']) ) {
      $nextPageToken=$playlistItems['nextPageToken'];
    }
    if( isset($playlistItems['prevPageToken']) ) {
     $prevPageToken=$playlistItems['prevPageToken'];
    }
   // working draft

} catch (Exception $exc) {
    //$debug_string= $exc->getTraceAsString();
    echo "Can't Load playlist.";
}

if($is_smartphone){
require_once ('index_view_mobile.php');
}else{
require_once ('index_view.php');
}

